/*
 * @Title HelloRMIPersonImpl.java
 * @Description
 * @Copyright Copyright(c) 2018
 * @Company com.lizhen
 * @author lizhen
 * @date 2018��10��23��
 * @version 0.0.1
 */
package com.lizhen.util.impl;

import com.lizhen.entity.Person;
import com.lizhen.util.HelloRMIPerson;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 * @author lizhen
 */
public class HelloRMIPersonImpl implements HelloRMIPerson {

    /**
     * @author lizhen
     */
    public HelloRMIPersonImpl() {
    }

    /**
     * @author lizhen
     */
    public static void main(String[] args) {
    }

    public List<Person> getPersons() throws Exception {
        List<Person> list = new ArrayList<Person>();
        String MYSQL_DRIVER = "com.mysql.jdbc.Driver";
        String MYSQL_URL = "jdbc:mysql://localhost:3306/lz";
        String MYSQL_USER = "root";
        String MYSQL_PSWD = "root";
        Connection conn = null;
        Statement stmt = null;
        Class.forName(MYSQL_DRIVER);

        System.out.println("Connecting to a selected database...");

        conn = DriverManager.getConnection(MYSQL_URL, MYSQL_USER, MYSQL_PSWD);

        System.out.println("Connected database successfully...");
        System.out.println("Creating statement...");

        stmt = conn.createStatement();
        String sql = "SELECT * FROM person";
        ResultSet rs = stmt.executeQuery(sql);
        while (rs.next()) {
            String name = rs.getString("name");
            Integer age = rs.getInt("age");
            Person person = new Person();
            person.setName(name);
            person.setAge(age);
            list.add(person);
        }
        rs.close();
        return list;
    }

}
